package routes

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"wskyygyso/api/app/http/controllers/api/v1/auth"
	category "wskyygyso/api/app/http/controllers/api/v1/category"
	link "wskyygyso/api/app/http/controllers/api/v1/link"
	topic "wskyygyso/api/app/http/controllers/api/v1/topic"
	"wskyygyso/api/app/http/controllers/api/v1/users"
	"wskyygyso/api/app/http/middlewares"
	"wskyygyso/api/pkg/config"
)

func RegisterApiRoutes(r *gin.Engine) {
	var v1 *gin.RouterGroup
	if len(config.Get("app.api_domain")) == 0 {
		v1 = r.Group("/v1")
	} else {
		v1 = r.Group("/api/v1")
	}

	{
		authGroup := v1.Group("/auth")
		authGroup.Use(middlewares.LimitIp("10000-H"))
		{
			suc := new(auth.SignUpController)
			authGroup.POST("/signup/phone/exist", suc.IsPhoneExist)
			authGroup.POST("/signup/email/exist", suc.IsEmailExist)
			authGroup.POST("/signup/using-phone", suc.SignupUsingPhone)
			authGroup.POST("/signup/using-email", suc.SignupUsingEmail)

			// 图片验证码，需要加限流
			captcha := new(auth.VerifyCodeController)
			authGroup.POST("/verify-codes/captcha", middlewares.LimitPerRoute("5-M"), captcha.ShowCaptcha)
			authGroup.POST("/verify-codes/phone", captcha.SendUsingPhone)
			authGroup.POST("/verify-codes/email", captcha.SendMail)

			//登录
			lgc := new(auth.LoginController)
			//手机验证码登录
			authGroup.POST("/login/using-phone", lgc.LoginByPhone)
			//密码登录
			authGroup.POST("/login/using-password", lgc.LoginByPassword)
			//token 刷新
			authGroup.POST("/login/refresh-token", lgc.RefreshToken)

			psw := new(auth.PassWordController)
			//手机修改密码
			authGroup.POST("/reset-password/using-phone", middlewares.AuthJwt(), psw.ResetPhone)
			//邮箱修改密码
			authGroup.POST("/reset-password/using-email", middlewares.AuthJwt(), psw.ResetEmail)

		}
	}

	{
		userGroup := v1.Group("/user")
		userGroup.Use(middlewares.LimitIp("10000-H"))
		{
			user := new(users.UsersController)
			userGroup.GET("/info", middlewares.AuthJwt(), user.CurrentUser)
			userGroup.GET("", middlewares.AuthJwt(), user.Index)
			userGroup.PUT("", middlewares.AuthJwt(), user.UpdateProfile)
			userGroup.PUT("/email", middlewares.AuthJwt(), user.UpdateEmail)
			userGroup.PUT("/phone", middlewares.AuthJwt(), user.UpdatePhone)
			userGroup.PUT("/password", middlewares.AuthJwt(), user.UpdatePassword)
			userGroup.PUT("/avatar", middlewares.AuthJwt(), user.UpdateAvatar)

		}
	}

	{
		categoryGroup := v1.Group("/category")
		categoryGroup.Use(middlewares.LimitIp("10000-H"))
		{
			cate := new(category.CategoriesController)
			categoryGroup.POST("", middlewares.AuthJwt(), cate.Store)
			categoryGroup.PUT("/:id", middlewares.AuthJwt(), cate.Update)
			categoryGroup.GET("", middlewares.AuthJwt(), cate.Index)
			categoryGroup.DELETE("/:id", middlewares.AuthJwt(), cate.Delete)
		}
	}

	{
		topicGroup := v1.Group("/topic")
		topicGroup.Use(middlewares.LimitIp("10000-H"))
		{
			topicController := new(topic.TopicsController)
			topicGroup.POST("", middlewares.AuthJwt(), topicController.Store)
			topicGroup.PUT("/:id", middlewares.AuthJwt(), topicController.Update)
			topicGroup.DELETE("/:id", middlewares.AuthJwt(), topicController.Delete)
			topicGroup.GET("", middlewares.AuthJwt(), topicController.Index)
			topicGroup.GET("/:id", middlewares.AuthJwt(), topicController.Show)
		}
	}

	{
		linkGroup := v1.Group("/link")
		linkGroup.Use(middlewares.LimitIp("10000-H"), middlewares.AuthJwt())
		{
			linkController := new(link.LinksController)
			linkGroup.GET("", linkController.Index)
			linkGroup.GET("/:id", linkController.Show)
			linkGroup.POST("", linkController.Store)
			linkGroup.PUT("/:id", linkController.Update)
			linkGroup.DELETE("/:id", linkController.Delete)
		}
	}

	v2 := r.Group("v2")
	{
		v2.POST("/", func(context *gin.Context) {
			context.JSON(http.StatusOK, gin.H{
				"msg": "hello world 2 !",
			})
		})
	}
}
